package com.ts.login.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ts.common.dto.Sms;
import com.ts.common.security.dto.UserToken;
import com.ts.common.utils.JWTUtils;
import com.ts.common.utils.SmsUtils;
import com.ts.login.dto.LoginDto;
import com.ts.login.dto.PhonePayload;
import com.ts.login.entity.User;
import com.ts.login.mapper.UserMapper;
import com.ts.login.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.model.AuthUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

/**
 * @author Zhendong Song
 * @date 2023/7/12
 * @description
 */

@Service
@Slf4j
public class LoginServiceImpl extends ServiceImpl<UserMapper, User> implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SmsUtils smsUtils;

    private long loginExpireTime = 8 * 60 * 60 * 1000;


    @Override
    public String loginByPhone(LoginDto<PhonePayload> dto) {
        // 1.判断这个手机号是否已经注册
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        String phone = dto.getPayload().getPhone();
        String code=dto.getPayload().getCode();
        LambdaQueryWrapper<User> eq = qw.eq(User::getPhone, phone);
        qw.eq(User::getCode,code);
        User user = userMapper.selectOne(eq);
        // 2、如果没有注册，自动注册一个新用户(用户名给一个默认值)
        // 3.如果这个手机号已经住注册了，认为登录成功
        if (user == null) {
            user = regisiter(phone);
        }
        // 4.登录成功后创建一个userToken对象
        UserToken userToken = new UserToken();
        userToken.setUsername(user.getName());
        userToken.setUserId(user.getId());
        userToken.setCreateTime(new Date());
        userToken.setExpireTime(null);
        // 5.把userToken对象揉入到JWT的字符串中
        return JWTUtils.createUserToken(userToken, loginExpireTime);
    }

    @Override
    public String loginByBaiDu(AuthUser baiduUser) {
        // 1.判断这个手机号是否已经注册
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        String baiduAuthId = baiduUser.getUuid();
        LambdaQueryWrapper<User> eq = qw.eq(User::getBaiduAuthId, baiduAuthId);
        User user = userMapper.selectOne(eq);
        if (user == null) {
            user = regisiterByBaidu(baiduUser);
        }
        // 4.登录成功后创建一个userToken对象
        UserToken userToken = new UserToken();
        userToken.setUsername(user.getName());
        userToken.setUserId(user.getId());
        userToken.setCreateTime(new Date());
        userToken.setExpireTime(null);
        // 5.把userToken对象揉入到JWT的字符串中
        return JWTUtils.createUserToken(userToken, loginExpireTime);
    }

    @Override
    public User regisiter(String phone) {
        User user = new User();
        user.setPhone(phone);
        user.setName(phone.substring(phone.length() - 4));
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return user;
        }
        return null;
    }
    @Override
    public User regisiterByBaidu(AuthUser authUser) {
        User user = new User();
        user.setBaiduAuthId(authUser.getUuid());
        user.setName(authUser.getUsername());
        int insert = userMapper.insert(user);
        if (insert > 0) {
            return user;
        }
        return null;
    }

    @Override
    public void codeSend(Sms sms) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        String phone=sms.getPhone();
        LambdaQueryWrapper<User> eq = qw.eq(User::getPhone,phone);
        User user = userMapper.selectOne(eq);
        if (user == null) {
            user = regisiter(phone);
        }
        String code=randomCode();
        user.setCode(code);
        baseMapper.updateById(user);
        sms.setContent(code);
        String s=JSON.toJSONString(sms);
        smsUtils.send(s);
    }
    public String randomCode(){
        String code = "";
        for (int i = 0; i < 4; i++) {
            int num = new Random().nextInt(10);
            code+=num;
        }
        return code;
    }
}
